package com.tech.langk.testhttps;

import android.app.TimePickerDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Date;
import java.util.List;

/**
 * Created by LangK on 2017/5/27.
 */

public class TimeAdapter extends BaseAdapter {

    TimePickerDialog dialog;

    public List<AutoTime> getList() {
        return list;
    }

    public void setList(List<AutoTime> list) {
        this.list = list;
    }

    private List<AutoTime> list;
    private Context context;

    public TimeAdapter(List<AutoTime> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (list!=null)return list.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list!=null)return list.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView ==null)
        {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_redtime,null);
            holder.name = (EditText) convertView.findViewById(R.id.name);
            holder.detail = (EditText) convertView.findViewById(R.id.detail);
            holder.moneyNumberText = (EditText) convertView.findViewById(R.id.moneyNumber);
            holder.shareMoneyNumberText = (EditText) convertView.findViewById(R.id.shareMoneyNumber);
            holder.moneyText = (EditText) convertView.findViewById(R.id.money);
            holder.shareMoneyText = (EditText) convertView.findViewById(R.id.shareMoney);
            holder.startTime = (Button) convertView.findViewById(R.id.time1);
            holder.endTime = (Button) convertView.findViewById(R.id.time2);
            convertView.setTag(holder);
        }else
        {
            holder = (Holder)convertView.getTag();
        }
        if (list.get(position).getEndTime()!=null){
            holder.endTime.setText(list.get(position).getEndTime());
        }
        if (list.get(position).getStartTime()!=null){
            holder.startTime.setText(list.get(position).getStartTime());
        }
        if (list.get(position).getMoney()!=null){
            holder.moneyText.setText(list.get(position).getMoney());
        }
        if (list.get(position).getShareMoney()!=null){
            holder.shareMoneyText.setText(list.get(position).getShareMoney());
        }
        if (list.get(position).getMoneyNumber()!=null){
            holder.moneyNumberText.setText(list.get(position).getMoneyNumber());
        }
        if (list.get(position).getShareMoneyNumber()!=null){
            holder.shareMoneyNumberText.setText(list.get(position).getShareMoneyNumber());
        }
        if (list.get(position).getName()!=null){
            holder.name.setText(list.get(position).getName());
        }
        if (list.get(position).getDetail()!=null){
            holder.detail.setText(list.get(position).getDetail());
        }


        holder.endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Date date = new Date();
                dialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        list.get(position).setEndTime(hourOfDay+":"+minute);
                        ((Button)v).setText(list.get(position).getEndTime());
                    }
                },date.getHours(),date.getMinutes(),true);
                dialog.show();
            }
        });
        holder.startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Date date = new Date();
                dialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        list.get(position).setStartTime(hourOfDay+":"+minute);
                        ((Button)v).setText(list.get(position).getStartTime());
                    }
                },date.getHours(),date.getMinutes(),true);
                dialog.show();
            }
        });

        holder.moneyText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setMoney(s.toString());
            }
        });
        holder.moneyNumberText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setMoneyNumber(s.toString());
            }
        });

        holder.shareMoneyText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setShareMoney(s.toString());
            }
        });
        holder.shareMoneyNumberText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setShareMoneyNumber(s.toString());
            }
        });

        holder.name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setName(s.toString());
            }
        });

        holder.detail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setDetail(s.toString());
            }
        });
        return convertView;
    }

    class Holder{
        EditText moneyText;
        EditText moneyNumberText;
        EditText shareMoneyNumberText;
        EditText shareMoneyText;
        EditText name;
        EditText detail;
        Button startTime;
        Button endTime;
    }
}
